<template>
	<view class="managerLogin">
		<view class="formBox mTop4">
			<view class="item">
				<view class="label">所属学校</view>
				<view class="inputBox flex1">
					<picker mode="selector" :range="schoolList" range-key='nickname' :value="schoolIndex" @change="schoolChange">
						<view class="picker flex_conter">
							<input class="input picker-input flex1" type="text" v-model="form.school" placeholder="请选择学校" placeholder-class="placeholderClase" />
							<view class="input-icon">
								<uni-icons type="arrowdown" size="13" color="#c0c4cc"></uni-icons>
							</view>
						</view>
					</picker>
				</view>
			</view>
			<view class="item mTop2">
				<view class="label">院系专业</view>
				<view class="inputBox flex1">
					<input class="input redText" type="text" v-model="form.special_name" placeholder="请如实填写" placeholder-class="placeholderClase"/>
				</view>
			</view>
			<view class="item mTop2">
				<view class="label">姓名</view>
				<view class="inputBox flex1">
					<input class="input redText" type="text" v-model="form.name" placeholder="请如实填写" placeholder-class="placeholderClase"/>
				</view>
			</view>
			<view class="item mTop2">
				<view class="label">手机号</view>
				<view class="inputBox flex1">
					<input class="input redText" type="text" v-model="form.phone" placeholder="请输入手机号" placeholder-class="placeholderClase"/>
				</view>
			</view>
		</view>
		<view class="bottomBox mTop5">
			<button class="btn" type="default" hover-class="none" @click="apply">确认申请</button>
		</view>
		
		
		<!-- <view class="serviceBox flex_space_b">
			<view class="item" @click="makePhoneCall('13523121288')">
				<uni-icons type="phone-filled"></uni-icons>
				<text class="text">驻马店师范学院13523121288</text>
			</view>
		</view>
		<view class="serviceBox flex_space_b">
			<view class="item" @click="makePhoneCall('18339429977')">
				<uni-icons type="phone-filled"></uni-icons>
				<text class="text">驻马店师范学院18339429977</text>
			</view>
		</view> -->
		<uni-popup ref="QRcodePopup" type="center" :isMaskClick="false">
			<view class="QRcodePopupBox">
				<view class="QRcodeBox">
					<image class="image" :src="qrcode" mode="widthFix"></image>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	var util = require('../../common/util.js');
	import { encryptData, decryptedData } from '../../utils/crypto.js'
	
	export default {
		data() {
			return {
				form: {
					special_name:'',
					name: '',
					phone: '',
					school:'',
					school_id:0
				},
				parent_user:'',
				is_login:0,
				qrcode:'',
				schoolList: [],
				schoolIndex: 0,
				
			}
		},
		onLoad(e) {
			this.is_login = e.is_login;
			if(this.is_login != 1){
				// this.login();
			}
			this.getLoadData();
		},
		methods: {
			makePhoneCall (phone) {
				uni.makePhoneCall({
					phoneNumber: phone
				});
			},
			//截取code
			getUrlCode() { 
				// 截取url中的code方法
					var url = location.search
					var theRequest = new Object()
					if (url.indexOf("?") != -1) {
						var str = url.substr(1)
						var strs = str.split("&");
						for (var i = 0; i < strs.length; i++) {
							theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1])
						}
					}
					return theRequest
					console.log(theRequest)
			},
			getLoadData(){
				util.requestPost('index/getSchoolList', {}, (res) => {
					this.schoolList = res.data.list;
				}, (err) => {
				}, {isShowLoading: false});
			},
			schoolChange (e) {
				let index = e.target.value;
				this.schoolIndex = index;
				this.form.school = this.schoolList[index]['nickname'];
				this.form.school_id = this.schoolList[index]['id'];
			},
			login() {
				var code = this.getUrlCode().code;
				if (code == null || !code) {
					//如果没有code 去获取code
					console.log('code', code);
					util.requestPost('user/getCodeUrl', {type:'getCodeUrl'}, (res) => {
						if (res.code == 1) {
							let url = res.data.list;
							window.location.href = url;
						} else {
							console.log(res);
						}
					}, (err) => {
						console.log(err);
					}, {isShowLoading: false});
					
				}else{
					//获取到code后的逻辑
					util.requestPost('user/login', {code:code,type:'applyOne'}, (res) => {
						if (res.code == 1) {
							uni.setStorageSync('utoken','');
							uni.setStorageSync('utoken', res.data.list);
							if(res.data.qrcode && res.data.qrcode != ''){
								this.$toast('请长按识别关注公众号')
								this.qrcode = res.data.qrcode;
								setTimeout(() => {
										this.$refs.QRcodePopup.open()
								}, 650);
								
							}else{
								setTimeout(() => {
									uni.navigateTo({
										url:'/pages/my/applyOne?is_login=1',
										fail(err) {
											console.log(err);
										}
									})
								}, 500);
							}
						} else {
							console.log(res);
						}
					}, (err) => {
						console.log(err);
					}, {isShowLoading: false});
				}
			},
			apply(){
				let form = Object.assign({}, this.form)
				let name = encryptData(form.name)
				let phone = encryptData(form.phone)
				form.name = name
				form.phone = phone
				
				util.requestPost('user/applyOne', {
					form: JSON.stringify(form),
					parent_user: this.parent_user,
				}, (res) => {
					if (res.code == 1) {
						console.log(res);
						 this.$toast(res.msg)
						 setTimeout(() => {
							uni.navigateTo({
								url:'/pages/my/my',
								fail(err) {
									console.log(err);
								}
							})
						 }, 500);
					}else if(res.code == 102){
						
						 this.$toast(res.msg)
						 
					}else {
						 this.$toast(res.msg)
					}
				}, (err) => {
					console.log(err);
				}, {isShowLoading: false});
			},
		}
	}
</script>

<style scoped lang="scss">
.QRcodePopupBox {
	padding: 40rpx;
	text-align: center;
	border-radius: 20rpx;
	background-color: #FFFFFF;
	position: relative;
	.close {
		position: absolute;
		right: 50%;
		bottom: -80rpx;
		transform: translateX(50%);
		width: 52rpx;
		height: 52rpx;
		line-height: 52rpx;
		text-align: center;
		font-size: 24rpx;
		border-radius: 50%;
		background-color: #FFFFFF;
	}
	.QRcodeBox {
		.image {
			width: 400rpx;
		}
	}
}
.serviceBox {
		margin-top:80rpx;
		.item {
			box-sizing: border-box;
			width: 700rpx;
			padding: 20rpx 0;
			font-size: 28rpx;
			text-align: center;
			border: $border-base;
			border-radius: 8rpx;
			.text {
				margin-left: 20rpx;
			}
			margin-bottom: 20rpx;
		}
	}
.managerLogin {
	padding: 20rpx;
	.formBox {
		.item {
			border: $border-base;
			border-radius: 12rpx;
			display: flex;
			align-items: stretch;
			.label {
				width: 280rpx;
				font-size: 28rpx;
				text-align: center;
				height: 68rpx;
				line-height: 68rpx;
				border-right: $border-base;
				background-color: $bg-color;
			}
		}
		.inputBox {
			.input {
				font-size: 28rpx;
				height: 68rpx;
				line-height: 68rpx;
				padding: 0 20rpx;
			}
			.input-icon {
				margin: 0 20rpx;
			}
			.picker-input {
				pointer-events: none;
			}
			.placeholderClase {
				font-size: 24rpx;
			}
		}
	}
	.bottomBox {
		.btn {
			color: #FFFFFF;
			font-size: 28rpx;
			line-height: 76rpx;
			border-radius: 20rpx;
			background: $btn-bg-color;
			&::after {
				border: none;
			}
		}
	}
}
</style>
